
#include "World/World.h"
#include "SDL_collide.h"


#include "Dynamic/include/DynamicOrganismObserver.h"
#include "Dynamic/include/DynamicWorldObserver.h"
#include "Dynamic/include/DynamicSharedData.h"


DynamicOrganismObserver::DynamicOrganismObserver( )
{

}

DynamicOrganismObserver::~DynamicOrganismObserver()
{
	// nothing to do.
}

void DynamicOrganismObserver::reset()
{
	// nothing to do.
}

void DynamicOrganismObserver::step()
{
	updateAllOrganismsPenalty();
}

void DynamicOrganismObserver::updateAllOrganismsPenalty() {
	std::vector<Organism*> allOrganisms=*(Organism::getAllOrganisms());
//	std::cout<<allOrganisms.size()<<" organism in the arena"<<std::endl;
	for (size_t i = 0; i < allOrganisms.size(); i++) {
		double penalty=-1;
	    std::vector<RobotAgentPtr>::iterator it;
	    for (it = allOrganisms[i]->agents.begin(); it != allOrganisms[i]->agents.end(); it++) {
	    	DynamicAgentWorldModel *currentAgentWorldModel = dynamic_cast<DynamicAgentWorldModel*> ((*it)->getWorldModel());

	    	//Inside the same organism we have the same penalty so if it's not already set we should set it.
	    	if(penalty==-1){
	    		penalty=DynamicWorldObserver::computePenalty(*it);
//	    		std::cout<<std::endl;
//	    		std::cout<<"Organism size: "<<(*it)->getOrganism()->size()<<std::endl;
	    	}

	    	currentAgentWorldModel->setPenalty(penalty);
//			std::cout<<currentAgentWorldModel->_agentId<<" has "<<currentAgentWorldModel->getPenalty()<<" of penalty"<<std::endl;
	    }
	}
}
